//package com.hyg.common.view.popup;
//
//import android.content.Context;
//import android.graphics.Color;
//import android.graphics.drawable.ColorDrawable;
//import android.support.annotation.NonNull;
//import android.support.v7.widget.LinearLayoutManager;
//import android.support.v7.widget.RecyclerView;
//import android.view.Gravity;
//import android.view.LayoutInflater;
//import android.view.View;
//import android.view.ViewGroup;
//import android.widget.PopupWindow;
//import android.widget.TextView;
//
//import java.util.List;
//
//import io.yingtai.openlive.bean.liveroom.LiveQuickQuantity;
//
//public class GiftNumberPopup extends PopupWindow {
//    private Context context;
//    private List<LiveQuickQuantity> rewardQuickQuantityList;
//    private NumberQuickCallback numberQuickCallback;
//
//    public GiftNumberPopup(Context context, List<LiveQuickQuantity> rewardQuickQuantityList, NumberQuickCallback numberQuickCallback) {
//        super(context);
//        this.context = context;
//        this.rewardQuickQuantityList = rewardQuickQuantityList;
//        this.numberQuickCallback = numberQuickCallback;
//        setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
//        setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
//        setOutsideTouchable(true);
//        setFocusable(true);
//        setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
//        View contentView = LayoutInflater.from(context).inflate(R.layout.ytlive_popup_gift_number_list,null, false);
//        setContentView(contentView);
//        setAnimationStyle(R.style.ActionSheetDialogAnimation);
//
//        initData();
//    }
//
//    private void initData(){
//        RecyclerView recyclerView = getContentView().findViewById(R.id.recyclerview);
//        recyclerView.setLayoutManager(new LinearLayoutManager(context));
////        recyclerView.addItemDecoration(new DividerItemDecoration(context, DividerItemDecoration.VERTICAL));
//        GiftNumberAdapter adapter = new GiftNumberAdapter(rewardQuickQuantityList,numberQuickCallback);
//        recyclerView.setAdapter(adapter);
//    }
//
//    /**
//     *  在android7.0上，如果不主动约束PopuWindow的大小，比如，设置布局大小为 MATCH_PARENT,那么PopuWindow会变得尽可能大，以至于 view下方无空间完全显示PopuWindow，而且view又无法向上滚动，此时PopuWindow会主动上移位置，直到可以显示完全。
//     *　解决办法：主动约束PopuWindow的内容大小，重写showAsDropDown方法：
//     * @param anchor
//     */
//    @Override
//    public void showAsDropDown(View anchor) {
////        if(Build.VERSION.SDK_INT >= 24){
////            Rect visibleFrame = new Rect();
////            anchor.getGlobalVisibleRect(visibleFrame);
////            int height = anchor.getResources().getDisplayMetrics().heightPixels - visibleFrame.bottom;
////            setHeight(height);
////        }
//        //测量view 注意这里，如果没有测量  ，下面的popupHeight高度为-2  ,因为LinearLayout.LayoutParams.WRAP_CONTENT这句自适应造成的
//        getContentView().measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
//        int popupWidth = getContentView().getMeasuredWidth();    //  获取测量后的宽度
//        int popupHeight = getContentView().getMeasuredHeight();  //获取测量后的高度
//        int[] location = new int[2];
//        // 获得位置 这里的v是目标控件，就是你要放在这个v的上面还是下面
//        anchor.getLocationOnScreen(location);
//        //这里就可自定义在上方和下方了 ，这种方式是为了确定在某个位置，某个控件的左边，右边，上边，下边都可以
//        showAtLocation(anchor, Gravity.NO_GRAVITY, (location[0] + anchor.getWidth() / 2) - popupWidth / 2, location[1] - popupHeight);
//    }
//
//    class GiftNumberAdapter extends RecyclerView.Adapter<GiftNumberAdapter.MyViewHolder>{
//        private List<LiveQuickQuantity> rewardQuickQuantityList;
//        private NumberQuickCallback numberQuickCallback;
//
//        public GiftNumberAdapter(List<LiveQuickQuantity> rewardQuickQuantityList, NumberQuickCallback numberQuickCallback) {
//            this.rewardQuickQuantityList = rewardQuickQuantityList;
//            this.numberQuickCallback = numberQuickCallback;
//        }
//
//        @NonNull
//        @Override
//        public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
//            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.ytlive_item_gift_number,parent,false);
//            return new MyViewHolder(view);
//        }
//
//        @Override
//        public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
//            LiveQuickQuantity quickQuantity = rewardQuickQuantityList.get(position);
//            holder.viewLine.setVisibility(position == 0?View.GONE:View.VISIBLE);
//            holder.tvNumber.setText(quickQuantity.getQuantity());
//            holder.tvName.setText(quickQuantity.getDescribe());
//            holder.itemView.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    dismiss();
//                    if(numberQuickCallback != null){
//                        numberQuickCallback.result(quickQuantity);
//                    }
//                }
//            });
//        }
//
//        @Override
//        public int getItemCount() {
//            return rewardQuickQuantityList.size();
//        }
//
//        class MyViewHolder extends RecyclerView.ViewHolder{
//            public TextView tvNumber;
//            public TextView tvName;
//            public TextView viewLine;
//
//            public MyViewHolder(@NonNull View itemView) {
//                super(itemView);
//                tvNumber = itemView.findViewById(R.id.tv_number);
//                tvName = itemView.findViewById(R.id.tv_name);
//                viewLine = itemView.findViewById(R.id.view_line);
//            }
//        }
//    }
//
//    @Override
//    public void dismiss() {
//        super.dismiss();
//        if(numberQuickCallback != null){
//            numberQuickCallback.dismiss();
//        }
//    }
//
//    public interface NumberQuickCallback{
//        void result(LiveQuickQuantity quickQuantity);
//
//        void dismiss();
//    }
//}
